﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PMToolTemplate {

	#region 添加控件需要更新的地方
	//TODO 另外 PanelMakerEditor的PMViewData也需要
	static string GetTypeParmsType(string type){
		string value = "string";
		switch (type) {
		case "UISprite":
		case "UILabel":
			value = "string";
			break;
		case "UISlider":
			value = "float";
			break;
		case "UIToggle":
			value = "bool";
			break;
		case "BoxCollider":
			value = "NGUIAutoNotifyData";
			break;
		}
		return value;
	}

	static string GetBaseMode(string viewname){
		string value = "";
		GameObject obj = GameObject.Find (viewname);
		value += GetBindDataComponent<UISprite> (obj);
		value += GetBindDataComponent<UILabel> (obj);
		value += GetBindDataComponent<UISlider> (obj);
		value += GetBindDataComponent<UIToggle> (obj);
		return value;
	}

	static string GetViewMode(string viewname){
		string value = "";
		GameObject obj = GameObject.Find (viewname);
		value += GetViewModeBindView (viewname);
		value += GetOnValueChangeComponent<UISprite> (obj);
		value += GetOnValueChangeComponent<UILabel> (obj);
		value += GetOnValueChangeComponent<UISlider> (obj);
		value += GetOnValueChangeComponent<UIToggle> (obj);
		value += GetViewComponent_Button<BoxCollider> (obj);
		return value;
	}

	static string GetViewModeBindView(string viewname){
		string value = "";
		string path = PMConst.PMTemplate +"/ViewModeBind.txt";
		string tmvalue = PMTool.LoadText (path);
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		GameObject obj = GameObject.Find (viewname);
		value += GetViewModeBindViewBind<UISprite> (obj);
		value += GetViewModeBindViewBind<UILabel> (obj);
		value += GetViewModeBindViewBind<UISlider> (obj);
		value += GetViewModeBindViewBind<UIToggle> (obj);
		replace.Add ("Info", value);
		tmvalue = PMTool.ReplaceScript (tmvalue, replace);
		return tmvalue;
	}

	static string GetBaseInfo(string viewname){
		string value = "";
		GameObject obj = GameObject.Find (viewname);
		value += GetViewComponent<UISprite> (obj);
		value += GetViewComponent<UILabel> (obj);
		value += GetViewComponent<UISlider> (obj);
		value += GetViewComponent<UIToggle> (obj);
		value += GetViewComponent_Button<BoxCollider> (obj,true);
		return value;
	}
	#endregion

	public static void CreateScriptModuleCtrl(string path,string name,bool check = false){
		string modulename = name + "Control";
		path = path + modulename + ".cs";
		if (check && PMTool.HasFile (path))
			return;
		string value = PMTool.LoadText (PMConst.PMPath+"Template/ModuleCtrl.txt");
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		
		replace.Add ("ClassName", modulename);
		replace.Add ("ModuleName", name);
		value = PMTool.ReplaceScript (value, replace);

		PMTool.SaveScript (path, value);
	}
	public static void CreateScript_View(string path,string name,bool check = false){
		string modulename = name + "_View";	
		path = path + modulename + ".cs";
		if (check && PMTool.HasFile (path))
			return;

		string value;
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string basemodulename;
//		string summary = "";

		replace.Clear ();
		value = PMTool.LoadText (PMConst.PMPath+"Template/ModulePanel.txt");
		basemodulename = name + "_ViewBase";
		replace.Add ("ClassName", modulename);
		replace.Add ("Extern", basemodulename);
		value = PMTool.ReplaceScript (value, replace);
		PMTool.SaveScript (path, value);
	}
	public static void CreateScript_ViewModel(string path,string name,bool check = false){
		string modulename = name + "_ViewModel";	
		path = path + modulename + ".cs";
		if (check && PMTool.HasFile (path))
			return;
		
		string value;
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string basemodulename;
		string summary = "";

		replace.Clear ();
		value = PMTool.LoadText (PMConst.PMPath+"Template/DefaultClass.txt");
		basemodulename =": "+ name + "_ViewModeBase";		
		summary = "override BindView";
		replace.Add ("ClassName", modulename);
		replace.Add ("Extern", basemodulename);
		replace.Add ("Summary", summary);
		value = PMTool.ReplaceScript (value, replace);
		PMTool.SaveScript (path, value);	}
	public static void CreateScript_Model(string path,string name,bool check = false){
		string modulename = name + "_Model";	
		path = path + modulename + ".cs";
		if (check && PMTool.HasFile (path))
			return;

		string value;
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string basemodulename;
		string summary = "";

		replace.Clear ();
		value = PMTool.LoadText (PMConst.PMPath+"Template/DefaultClass.txt");
		basemodulename =": "+ name + "_ModeBase";		
		summary = "public BindData<string> Lab_Name = new BindData<string>()";
		replace.Add ("ClassName", modulename);
		replace.Add ("Extern", basemodulename);
		replace.Add ("Summary", summary);
		value = PMTool.ReplaceScript (value, replace);
		PMTool.SaveScript (path, value);
	}

	public static void FixBaseView_ViewMode_Mode(string path,string name){
		string value = "";
//		string oldstr = "";
//		string newstr = "class " + name + " : " + name + "Base";
//		string baseviewpath = path + name + "Base.cs";
//		Dictionary<string,string> replace = new Dictionary<string, string> ();

		value = PMTool.LoadText (PMConst.PMPath+"Template/ModulePanelBase.txt");

		value = ReplaceBaseView (value ,name);
		value = ReplaceMode (value ,name);
		value = ReplaceViewMode (value, name);
		PMTool.SaveScript (path+name+"Base.cs", value);
	}

	public static string ReplaceViewMode(string source,string name){
		string value = PMTool.LoadText (PMConst.PMTemplate + "/ViewMode.txt");
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		replace.Add ("ClassName", name + "Mode");
		replace.Add ("ViewName",name.Split('_')[0]);
		replace.Add ("Info", GetViewMode (name));
		value = PMTool.ReplaceScript (value, replace);

		replace.Clear ();
		replace.Add ("ViewMode", value);
		source = PMTool.ReplaceScript (source, replace);
		return source;
	}

	public static string ReplaceMode(string source,string name){
		string value = PMTool.LoadText (PMConst.PMTemplate + "/Mode.txt");

		Dictionary<string,string> replace = new Dictionary<string, string> ();
		replace.Add ("ClassName", name.Split('_')[0]+"_Mode");
		replace.Add ("Info", GetBaseMode (name));
		value = PMTool.ReplaceScript (value, replace);

		replace.Clear ();
		replace.Add ("Mode", value);
		source = PMTool.ReplaceScript (source, replace);
		return source;
	}


	public static string ReplaceBaseView(string source,string name){
		string value = PMTool.LoadText (PMConst.PMTemplate + "/BaseView.txt");
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		replace.Add ("ClassName", name + "Base");
		replace.Add ("Info", GetBaseInfo (name));
		replace.Add ("Init", GetViewBaseInit (name));
		value = PMTool.ReplaceScript (value, replace);

		replace.Clear ();
		replace.Add ("BaseView", value);
		source = PMTool.ReplaceScript (source, replace);
		return source;
	}



	public static void FixViewUI(string path,string name){
		
		try{
		string value = PMTool.LoadText (path+name+".cs");
		string oldstr = "";
		string newstr = "class " + name + " : " + name + "Base";

		oldstr = "class " + name + " : ViewUI";
		if(value.Contains(oldstr)){
			value = value.Replace (oldstr,newstr);
		}


		oldstr = "class " + name + " : MonoBehaviour";
		if(value.Contains(oldstr)){
			value = value.Replace (oldstr,newstr);
		}
		PMTool.SaveScript (path + name + ".cs", value);
		}catch{
		}
	}

	static string GetViewBaseInit(string viewname){
		viewname = viewname.Split ('_') [0];
		string path = PMConst.PMTemplate +"/BaseViewInit.txt";
		string tmvalue = PMTool.LoadText (path);
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		replace.Add ("ViewName", viewname);
		replace.Add ("VNameUnBase", viewname.Replace("Base","")+"_View");
		tmvalue = PMTool.ReplaceScript (tmvalue, replace);
		return tmvalue;
	}



	static string GetMode(string valuename){
		string value = "";
		return value;
	}

	static string GetViewModeInfo(string valuename){
		string value = "";
		return value;
	}
	#region 组件的批量化处理
	static string GetViewModeBindViewBind<T>(GameObject obj)where T:MonoBehaviour{
		if (obj == null)
			return "";
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string value = "";
		string tmvalue = "\t\tmymodel.Value.%ObjName.onVlaueChange += On%ObjNameChange;\n";
		string typename = PMTool.GetUIComponentTag<T> ();
		T[] ts = obj.GetComponentsInChildren<T> ();
		MyOBJ myobj = obj.GetComponent<MyOBJ> ();
		for (int i = 0; i < ts.Length; i++) {
			string objname = ts [i].gameObject.name;
			if (objname.Contains (typename)) {
				objname = FixComponentName (typename,objname);
				replace.Add ("ObjName", objname);
				value += PMTool.ReplaceScript (tmvalue, replace);
				replace.Remove ("ObjName");
				myobj.AddObj (objname, ts [i].gameObject);
			}
		}
		return value;	

	}

	static string GetBindDataComponent<T>(GameObject obj)where T:MonoBehaviour{
		if (obj == null)
			return "";
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string value = "";
		string BindTypeName = GetTypeParmsType(typeof(T).Name);

		string path = PMConst.PMTemplate +"/BindData.txt";
		string tmvalue = PMTool.LoadText (path);
		string typename = PMTool.GetUIComponentTag<T> ();
		T[] ts = obj.GetComponentsInChildren<T> ();
		MyOBJ myobj = obj.GetComponent<MyOBJ> ();
		for (int i = 0; i < ts.Length; i++) {
			string objname = ts [i].gameObject.name;
			if (objname.Contains (typename)) {
				replace.Add ("type",BindTypeName);
				objname = FixComponentName (typename,objname);
				replace.Add ("membername", objname);
				value += PMTool.ReplaceScript (tmvalue, replace);
				replace.Remove ("type");
				replace.Remove ("membername");
				myobj.AddObj (objname, ts [i].gameObject);
			}
		}
		return value;
	}

	static string GetOnValueChangeComponent<T>(GameObject obj)where T:MonoBehaviour{
		if (obj == null)
			return "";
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string value = "";
		string path = PMConst.PMTemplate +"/ViewModeValueChange.txt";
		string tmvalue = PMTool.LoadText (path);
		string typename = PMTool.GetUIComponentTag<T> ();
		T[] ts = obj.GetComponentsInChildren<T> ();
		MyOBJ myobj = obj.GetComponent<MyOBJ> ();
		for (int i = 0; i < ts.Length; i++) {
			string objname = ts [i].gameObject.name;
			if (objname.Contains (typename)) {
				objname = FixComponentName (typename,objname);
				replace.Add ("ObjName", objname);
				replace.Add("type",GetTypeParmsType(typeof(T).Name));
				value += PMTool.ReplaceScript (tmvalue, replace);
				replace.Remove ("ObjName");
				replace.Remove ("type");
				myobj.AddObj (objname, ts [i].gameObject);
			}
		}
		return value;	
	}
	static string GetViewComponent_Button<T>(GameObject obj,bool call = false)where T:Component{
		if (obj == null)
			return "";
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string value = "";

		string path = PMConst.PMTemplate +"/ComponentButton.txt";

		string tmvalue = PMTool.LoadText (path);
		string typename = PMTool.GetUIComponentTag<T> ();

		T[] ts = obj.GetComponentsInChildren<T> ();
		MyOBJ myobj = obj.GetComponent<MyOBJ> ();
		for (int i = 0; i < ts.Length; i++) {
			string objname = ts [i].gameObject.name;
			typename = "btn";
			if (objname.Contains (typename)) {
				objname = FixComponentName (typename,objname);
				string voidname = "Click_" + objname.Replace ("btn", "");
				replace.Add ("VoidName", voidname);
				replace.Add("type",GetTypeParmsType(typeof(T).Name));
				replace.Add ("virtual", "virtual");
				replace.Add ("info",call?"viewmodel."+ voidname +"(value);":"");
				value += PMTool.ReplaceScript (tmvalue, replace);
				replace.Remove ("VoidName");
				replace.Remove ("type");
				replace.Remove ("virtual");
				replace.Remove ("info");
				myobj.AddObj (objname, ts [i].gameObject);
			}
		}
		return value;	
	}
	static string GetViewComponent<T>(GameObject obj)where T:MonoBehaviour{
		if (obj == null)
			return "";
		Dictionary<string,string> replace = new Dictionary<string, string> ();
		string value = "";

		string path = PMConst.PMTemplate +"/Component.txt";

		string tmvalue = PMTool.LoadText (path);
		string typename = PMTool.GetUIComponentTag<T> ();

		T[] ts = obj.GetComponentsInChildren<T> ();
		MyOBJ myobj = obj.GetComponent<MyOBJ> ();
		for (int i = 0; i < ts.Length; i++) {
			string objname = ts [i].gameObject.name;
			if (objname.Contains (typename)) {
				objname = FixComponentName (typename,objname);
				//				objname = objname.Replace (typename, "");
				replace.Add ("VoidName", "Set" + objname);
				replace.Add ("ObjectName", objname);
				replace.Add("type",GetTypeParmsType(typeof(T).Name));
				replace.Add("ComponentType",typeof(T).Name.Replace("UI",""));
				value += PMTool.ReplaceScript (tmvalue, replace);
				replace.Remove ("VoidName");
				replace.Remove ("ObjectName");
				replace.Remove ("type");
				replace.Remove ("ComponentType");
				myobj.AddObj (objname, ts [i].gameObject);
			}
		}
		return value;	
	}
	#endregion
	public static string FixComponentName(string typename,string obj){
		string[] sp = obj.Split ('_');

		return typename+sp [sp.Length - 1];
	}

}
